﻿/****************
* Authored by Zhuo "Wizcas" Chen
* 2018 © Wubi.fun all rights reserved
*****************/

using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ResItem : MonoBehaviour 
{
    public string id;
    [SerializeField] CanvasGroup ui;
    [SerializeField] Image icon;
    [SerializeField] Text amount;
    [SerializeField] Color positiveColor = Color.white;
    [SerializeField] Color negativeColor = Color.red;

    int _amt;
    RectTransform rectTransform { get { return transform as RectTransform; } }

    public void Refresh(Res res, bool showSign=false)
    {
        _amt = res.amount;
        icon.sprite = res.icon;
        amount.text = !showSign ? _amt.ToString() : _amt.ToString("+#;-#;0");
        amount.color = _amt >= 0 ? positiveColor : negativeColor;
    }

    public Tween Pop(Res res, Vector2 pos, int counter, PopSettings settings)
    {
        Refresh(res, true);
        rectTransform.anchoredPosition = pos;
        ui.alpha = 0;
        return DOTween.Sequence().SetDelay(settings.interval * counter)
            .AppendCallback(() => ui.alpha = 1)
            .Join(rectTransform.DOAnchorPosY(pos.y + settings.upY, settings.popTime).SetEase(Ease.OutSine))
            .Join(ui.DOFade(0, settings.fadeTime).SetDelay(settings.fadeDelay))
            .AppendCallback(() => Destroy(gameObject));
    }
}
